<template>
  <div class="modal">
    <el-dialog
      v-model="dialogVisible"
      :title="
        isNewRef ? modalConfig.header.newTitle : modalConfig.header.editTitle
      "
      width="500"
      center
    >
      <div class="form">
        <el-form size="large" :model="formData" label-width="80px">
          <template v-for="item in modalConfig.formItems" :key="item.prop">
            <el-form-item :label="item.label" :prop="item.prop">
              <template v-if="item.type === 'input'">
                <el-input
                  v-model="formData[item.prop]"
                  :placeholder="item.placeholder"
                />
              </template>
              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  v-model="formData[item.prop]"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </template>
              <template v-if="item.type === 'select'">
                <el-select
                  v-model="formData[item.prop]"
                  :placeholder="item.placeholder"
                  style="width: 100%"
                >
                  <template v-for="option in item.options" :key="option.value">
                    <el-option :label="option.label" :value="option.value">{{
                      option.label
                    }}</el-option>
                  </template>
                </el-select>
              </template>
              <template v-if="item.type === 'custom'">
                <slot :name="item.slotName"></slot>
              </template>
            </el-form-item>
          </template>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import useSystemStore from '@/store/main/system/system'

interface IModalProps {
  modalConfig: {
    pageName: string
    header: {
      newTitle: string
      editTitle: string
    }
    formItems: any[]
  }
  otherInfo?: any
}

const props = defineProps<IModalProps>()
//初始化表单数据，如果有初始值就用初始值，没有就用空字符串

const initialData: any = {}
for (const item of props.modalConfig.formItems) {
  initialData[item.prop] = item.initialValue ?? ''
}

const formData = reactive<any>({ ...initialData })

//用于传递函数参数，要编辑数据的id，新建时没有，编辑时有
const editData = ref()
//新建还是编辑状态，true为新建,false为编辑
const isNewRef = ref(true)
//获取角色数据
const systemStore = useSystemStore()
//表示一开始是否可见,参数区分是新建还是编辑
const dialogVisible = ref(false)

function setModalVisible(isNew: boolean = true, itemData: any) {
  dialogVisible.value = true
  isNewRef.value = isNew
  //编辑状态下
  if (!isNew && itemData) {
    for (const key in formData) {
      formData[key] = itemData[key]
    }

    editData.value = itemData
  } else {
    //新建状态下清空表单数据，防止在编辑之后点击新建后又保留了之前的表单数据
    for (const key in formData) {
      const item = props.modalConfig.formItems.find(
        (item: any) => item.prop === key
      ) as any

      formData[key] = item ? item.initialValue : ''
    }
    editData.value = null
  }
}
function handleConfirmClick() {
  dialogVisible.value = false
  let infoData = formData
  if (props.otherInfo) {
    infoData = { ...infoData, ...props.otherInfo }
  }
  if (!isNewRef.value && editData.value) {
    //编辑状态确定按钮点击事件
    systemStore.editPageDataAction(
      props.modalConfig.pageName,
      editData.value.id,
      infoData
    )
  } else {
    //新建状态确定按钮点击事件
    //传入页面名称，页面信息

    systemStore.newPageDataAction(props.modalConfig.pageName, infoData)
  }
}
defineExpose({ setModalVisible })
</script>

<style lang="less" scoped></style>
